﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;

namespace GoShopping.Web.UserControls
{
    public partial class ProductTypeNavigator : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //tblPrdtType.Width = Unit.Pixel(250);
            tblPrdtType.CssClass = "AutoNewline";
            tblPrdtType.CellPadding = 2;
            tblPrdtType.CellSpacing = 0;

            CreatePrdtDetailTable();
        }

        private void SetSubTypeList(IList<GoShopping.Model.ProductType> iList, GoShopping.Model.ProductType productType)
        {
            ArrayList al = new ArrayList();
            TableRow tblRow = new TableRow();
            tblRow.CssClass = "SmallType";
            TableCell tblCell = new TableCell();
            //tblCell.CssClass = "SmallType";
            string seperator = "  ";
            for (int i = 0; i < iList.Count; i++)
            {
                if (iList[i].ParentID == productType.ID)
                {
                    al.Add(iList[i]);
                }
            }
            int sum = 0;
            for (int i = 0; i < al.Count; i++)
            {
                tblRow.HorizontalAlign = HorizontalAlign.Left;
                HyperLink hLink = new HyperLink();
                hLink.CssClass = "n";
                hLink.ID = ((Model.ProductType)al[i]).ID.ToString();
                hLink.Text = ((Model.ProductType)al[i]).TypeName + seperator;
                sum += System.Text.Encoding.Default.GetBytes(hLink.Text).Length;
                if (sum > 31)
                {

                    hLink.Text = "<BR>" + hLink.Text;
                    sum = System.Text.Encoding.Default.GetBytes(hLink.Text).Length;
                }
                hLink.NavigateUrl = "~/PrdtListByClass.aspx?typeId=" + hLink.ID + "&typeName=" + ((Model.ProductType)al[i]).TypeName;
                tblCell.Controls.Add(hLink);
            }
            tblRow.Cells.Add(tblCell);
            tblPrdtType.Rows.Add(tblRow);
        }

        private void CreatePrdtDetailTable()
        {
            BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
            
            //IList<Model.ProductType> iList = ptb.GetAllProductTypeList();
            IList<Model.ProductType> iList = PrdtTypeList;
            for (int i = 0; i < iList.Count; i++)
            {
                if (iList[i].ParentID == -1)
                {
                    bool flag = false;
                    TableRow rowHeader = new TableRow();
                    rowHeader.CssClass = "BigType";
                    TableCell tblCell = new TableCell();
                    Label lblHeader = new Label();
                    foreach (Model.ProductType pt in iList)
                    {
                        if (pt.ParentID == iList[i].ID)
                        {
                            flag = true;
                            break;
                        }

                    }
                    if (flag)
                    {
                        lblHeader.Text = iList[i].TypeName;
                        tblCell.Controls.Add(lblHeader);

                        tblCell.HorizontalAlign = HorizontalAlign.Left;

                        TableCell filledCell = new TableCell();
                        //filledCell.Width = Unit.Pixel(150);
                        rowHeader.Cells.Add(tblCell);

                        tblPrdtType.Rows.Add(rowHeader);

                        SetSubTypeList(iList, iList[i]);
                    }
                }
            }
        }

        private IList<Model.ProductType> iList = new List<Model.ProductType>();

        public IList<Model.ProductType> PrdtTypeList
        {
            get { return iList; }
            set
            {
                if (value != null)
                {
                    iList = value;
                }
            }
        }
    }
}